package icu.qx.wm.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import icu.qx.common.utils.CodeUtils;
import icu.qx.wm.domain.WmStorageArea;
import icu.qx.wm.exception.WmStorageAreaException;
import icu.qx.wm.mapper.WmStorageAreaMapper;
import icu.qx.wm.service.IWmStorageAreaService;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * &#064;description  针对表【wm_storage_area(库区)】的数据库操作Service实现
 * &#064;createDate  2024-06-05 17:01:06
 */
@Service
public class WmStorageAreaServiceImpl implements IWmStorageAreaService {
    private final WmStorageAreaMapper wmStorageAreaMapper;
    private final CodeUtils codeUtils;

    @Autowired
    public WmStorageAreaServiceImpl(WmStorageAreaMapper wmStorageAreaMapper, CodeUtils codeUtils) {
        this.wmStorageAreaMapper = wmStorageAreaMapper;
        this.codeUtils = codeUtils;
    }

    /**
     * 查询库区
     *
     * @param saCode 库区主键
     * @return 库区
     */
    @Override
    public WmStorageArea selectWmStorageAreaBySaCode(String saCode) {
        return wmStorageAreaMapper.selectWmStorageAreaBySaCode(saCode);
    }

    /**
     * 查询库区列表
     *
     * @param wmStorageArea 库区
     * @return 库区
     */
    @Override
    public List<WmStorageArea> selectWmStorageAreaList(WmStorageArea wmStorageArea) {
        return wmStorageAreaMapper.selectWmStorageAreaList(wmStorageArea);
    }

    /**
     * 新增库区
     *
     * @param wmStorageArea 库区
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmStorageAreaException.class)
    public int insertWmStorageArea(WmStorageArea wmStorageArea) {
        try {
            compareArea(wmStorageArea.getSaWCode(), wmStorageArea.getSaArea());

            wmStorageArea.setCreateTime(DateUtils.getNowDate());

            String code = codeUtils.generateCode("wm_storage_area");
            wmStorageArea.setSaCode(code);

            return wmStorageAreaMapper.insertWmStorageArea(wmStorageArea);
        } catch (Exception e) {
            throw new WmStorageAreaException("插入库区失败: " + e.getMessage(), e);
        }
    }

    /**
     * 修改库区
     *
     * @param wmStorageArea 库区
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmStorageAreaException.class)
    public int updateWmStorageArea(WmStorageArea wmStorageArea) {
        try {
            compareArea(wmStorageArea.getSaWCode(), wmStorageArea.getSaArea());

            wmStorageArea.setUpdateTime(DateUtils.getNowDate());

            return wmStorageAreaMapper.updateWmStorageArea(wmStorageArea);
        } catch (Exception e) {
            throw new WmStorageAreaException("更新库区失败: " + e.getMessage(), e);
        }
    }

    private void compareArea(String wCode, Double saArea) {
        Double wArea = wmStorageAreaMapper.selectWmWarehouseArea(wCode);
        if (wArea < saArea) {
            throw new WmStorageAreaException("库区面积大于仓库面积");
        }
    }

    /**
     * 批量删除库区
     *
     * @param saCodes 需要删除的库区主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmStorageAreaException.class)
    public int deleteWmStorageAreaBySaCodes(String[] saCodes, Integer[] slCount) {
        try {
            for (Integer integer : slCount) {
                if (integer > 0) {
                    throw new WmStorageAreaException("当前库区存在库位");
                }
            }
            return wmStorageAreaMapper.deleteWmStorageAreaBySaCodes(saCodes);
        } catch (Exception e) {
            throw new WmStorageAreaException("删除库区失败: " + e.getMessage(), e);
        }
    }

    /**
     * 查询库存编码
     */
    @Override
    public List<Map<String, String>> selectStorageAreaCode(WarehouseFormVo warehouseFormVo) {
        return wmStorageAreaMapper.selectStorageAreaCode(warehouseFormVo);
    }
}